﻿using System.IO;
using System.Threading.Tasks;

namespace Sidvall.Net
{
    public class HttpContent : IHttpContent
    {
        #region Public Members

        #region Headers

        private HttpContentHeaders _Headers;
        public HttpContentHeaders Headers
        {
            get
            {
                return _Headers;
            }
            private set
            {
                _Headers = value;
            }
        }

        #endregion

        #region CopyToAsync

        public async Task CopyToAsync(Stream stream)
        {
            await this.Content.CopyToAsync(stream).ConfigureAwait(false);
        }

        #endregion
        #region ReadAsByteArrayAsync

        public async Task<byte[]> ReadAsByteArrayAsync()
        {
            return await this.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
        }

        #endregion
        #region ReadAsStreamAsync

        public async Task<Stream> ReadAsStreamAsync()
        {
            return await this.Content.ReadAsStreamAsync().ConfigureAwait(false);
        }

        #endregion
        #region ReadAsStringAsync

        public async Task<string> ReadAsStringAsync()
        {
            return await this.Content.ReadAsStringAsync().ConfigureAwait(false);
        }

        #endregion

        #endregion
        #region Private Members

        #region Content

        private System.Net.Http.HttpContent _Content;
        internal System.Net.Http.HttpContent Content
        {
            get
            {
                return _Content;
            }
            set
            {
                _Content = value;
                if ((value != null) && (value.Headers != null))
                {
                    this.Headers = new HttpContentHeaders()
                    {
                        ContentLength = value.Headers.ContentLength,
                        Expires = value.Headers.Expires,
                        LastModified = value.Headers.LastModified,
                    };
                    if (value.Headers.ContentDisposition != null)
                    {
                        this.Headers.ContentDisposition = new ContentDispositionHeaderValue()
                        {
                            CreationDate = value.Headers.ContentDisposition.CreationDate,
                            DispositionType = value.Headers.ContentDisposition.DispositionType,
                            FileName = value.Headers.ContentDisposition.FileName,
                            FileNameStar = value.Headers.ContentDisposition.FileNameStar,
                            ModificationDate = value.Headers.ContentDisposition.ModificationDate,
                            Name = value.Headers.ContentDisposition.Name,
                            ReadDate = value.Headers.ContentDisposition.ReadDate,
                            Size = value.Headers.ContentDisposition.Size,
                        };
                    }
                    if (value.Headers.ContentRange != null)
                    {
                        this.Headers.ContentRange = new ContentRangeHeaderValue()
                        {
                            From = value.Headers.ContentRange.From,
                            To = value.Headers.ContentRange.To,
                            Length = value.Headers.ContentRange.Length,
                        };
                    }
                    if (value.Headers.ContentType != null)
                    {
                        this.Headers.ContentType = new MediaTypeHeaderValue()
                        {
                            CharSet = value.Headers.ContentType.CharSet,
                            MediaType = value.Headers.ContentType.MediaType,
                        };
                        if (value.Headers.ContentType.Parameters != null)
                        {
                            foreach (var parameter in value.Headers.ContentType.Parameters)
                                this.Headers.ContentType.Parameters.Add(parameter.Name, parameter.Value);
                        }
                    }
                }
                else
                {
                    this.Headers = null;
                }
            }
        }

        #endregion

        #endregion
        #region Constructors

        public HttpContent()
        {
        }

        #endregion
    }
}
